package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.TemaDAORemote;
import proyecto.EntityBeans.Tema;

@Stateless
@Remote(TemaBLRemote.class)
public class TemaBL implements TemaBLRemote{

	TemaDAORemote temaDAORemote;
	
	public int save(Tema tema) {	
		
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return temaDAORemote.save(tema); //alta de tema		
		
	}
		
	public Tema findById(int id) {
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return temaDAORemote.findById(id);
		
	}
	
		
	public void merge(Tema tema) {
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
			temaDAORemote.merge(tema);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findByName(String name)
	{	
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return temaDAORemote.findByName(name);
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return temaDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return temaDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idTema){	
		try
		{
			Context context = new InitialContext();
			temaDAORemote = (TemaDAORemote) context.lookup("TemaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		temaDAORemote.remove(idTema);
	}
}
